package com.cbd.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbd.dao.CompanycarportDao;
import com.cbd.pojo.Companycarport;
import com.cbd.pojo.PageBean;
import com.cbd.util.IQuantity;
@Repository
public class CompanycarportDaoImpl implements CompanycarportDao {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	@Override
	public @ResponseBody PageBean findAllCarPort(int needPage,int companyId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createSQLQuery("select carportAddress,carportNO,pactNO,pactStartTime,pactEndTime"
					+" from companycarport ccp left join cbdpact cbdp ON ccp.pactID=cbdp.pactID"
					+" where cbdp.companyID=? limit ?,?");
		query.setParameter(0, companyId);
		query.setParameter(1, (needPage-1)*IQuantity.pageSize);
		query.setParameter(2, IQuantity.pageSize);
		Query query2=session.createQuery("select count(*) from Companycarport ccp where ccp.cbdpact.company.companyID=?");
		query2.setParameter(0, 1);
		PageBean pageBean = new PageBean();
		Long countPage =  (Long) query2.list().get(0);
		int countPg = Integer.parseInt(""+countPage);
		pageBean.setCountPage(countPg%IQuantity.pageSize==0?countPg/IQuantity.pageSize:(countPg/IQuantity.pageSize)+1);
		pageBean.setCurPage(needPage);
		pageBean.setPageSize(IQuantity.pageSize);
		pageBean.setList(query.list());
		return pageBean;
	}

	@Override
	public PageBean findUsableCarPort(int needPage,int companyId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		System.out.println("传入的企业ID"+companyId);
		Query query = session.createSQLQuery("select carportAddress,carportNO,pactEndTime"
					+" from companycarport ccp left join cbdpact cbdp ON ccp.pactID=cbdp.pactID"
					+" where cbdp.companyID=? and ccp.type=0 limit ?,?");
		query.setParameter(0, companyId);
		query.setParameter(1, (needPage-1)*IQuantity.pageSize);
		query.setParameter(2, IQuantity.pageSize);
		Query query2=session.createQuery("select count(*) from Companycarport ccp where ccp.cbdpact.company.companyID=? and ccp.type=0");
		query2.setParameter(0, 1);
		PageBean pageBean = new PageBean();
		Long countPage =  (Long) query2.list().get(0);
		int countPg = Integer.parseInt(""+countPage);
		pageBean.setCountPage((countPg%IQuantity.pageSize==0?countPg/IQuantity.pageSize:(int)(countPg/IQuantity.pageSize)+1));
		pageBean.setCurPage(needPage);
		pageBean.setPageSize(IQuantity.pageSize);
		pageBean.setList(query.list());
		return pageBean;
	}

	@Override
	public void addCompanycarport(Companycarport port) {
		Session session= sessionFactory.getCurrentSession();
		session.save(port);
	}

	@Override
	public List<Companycarport> findAllCarPort() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query =session.createQuery("from Companycarport car  where  car.cbdpact.type = false and car.type=false");
		return query.list();
	}
	@Override
	public void updateCarport(int parseInt){
		Session session = sessionFactory.getCurrentSession();
		Query query =session.createQuery("update Companycarport set companyPact = ? ,type= 0 where comPactID = ?");
		query.setParameter(0, null);
		query.setParameter(1, parseInt);
		query.executeUpdate();
	}

	@Override
	public void updateCbdCompanyCarport(Companycarport companycarport) {
		Session session = sessionFactory.getCurrentSession();
		Query query =session.createQuery("update Companycarport set cbdpact = ? ,type= ? where carportID = ?");
		query.setParameter(0, companycarport.getCbdpact());
		query.setParameter(1, companycarport.isType());
		query.setParameter(2, companycarport.getCarportID());
		query.executeUpdate();
		
	}

	@Override
	public List<Companycarport> findCompanyCarportByPactID(int parseInt) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query =session.createQuery(" from Companycarport where cbdpact.pactID = ? and companyPact !=null");
		query.setParameter(0, parseInt);
		return query.list();
	}

	
}
